/*
* Copyright (C) 2024-2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "configuratingsystemleveldialog.h"
#include "securitylevelchangedefinition.h"
#include "widgets/dspinner.h"
//#include <DFontSizeManager>
//#include <DLabel>
//#include <DSpinner>
//#include <DFrame>

//#include <QScroller>
#include <QLabel>
#include <QScrollBar>
#include <QScrollArea>
#include <QVBoxLayout>

ConfiguratingSystemLevelDialog::ConfiguratingSystemLevelDialog(QWidget *parent)
    : DDialog(parent)
{
    setAccessibleName("configuratingSystemLevelDialog");

    initUI();
}

void ConfiguratingSystemLevelDialog::initUI()
{
    Qt::WindowFlags flags = windowFlags();
    flags |= Qt::WindowStaysOnTopHint;
    flags &= (~Qt::WindowCloseButtonHint);
    setWindowFlags(flags);
//    setWindowFlag(Qt::WindowCloseButtonHint, false); // 不显示右上角关闭
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setModal(true);
    setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);
    setIcon(QIcon::fromTheme(DIALOG_TIP_YELLOW));

    // 创建控件及布局
    QWidget *contentWidget = new QWidget(this);
    contentWidget->setAccessibleName("configuratingSystemLevelDialog_contentWidget");
    QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
    contentWidget->setLayout(contentLayout);
    contentLayout->addSpacing(10);

    // 设置标题
    QLabel *title = new QLabel(this);
    title->setAccessibleName("configuratingSystemLevelDialog_title");
    QFont ft = title->font();
    ft.setBold(true);
    title->setFont(ft);
    title->setContentsMargins(20, 0, 0, 0);
    title->setText(tr("Configuring the security level, please do not close the window"));
    addContent(title, Qt::AlignCenter);

    // 嵌套滑动条
    QFrame *scFrame = new QFrame(this);
    scFrame->setAccessibleName("contentWidget_scrollAreaframe");
    scFrame->setContentsMargins(0, 0, 0, 0);
    QVBoxLayout *sctLayout = new QVBoxLayout(contentWidget);
    scFrame->setLayout(sctLayout);

    // 添加滑动条区域
    QScrollArea *scrollArea = new QScrollArea(contentWidget);
    scrollArea->setAccessibleName("scrollAreaframe_scrollArea");
    scrollArea->viewport()->setAccessibleName("scrollArea_viewWidget");
    scrollArea->setFrameShape(QFrame::Shape::NoFrame);
    scrollArea->setWidgetResizable(true);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
    scrollArea->setFixedHeight(140);
    sctLayout->addWidget(scrollArea);
    contentLayout->addWidget(scFrame);

//    // 触屏移动
//    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
//    QScroller *scroller = QScroller::scroller(scrollArea);
//    QScrollerProperties sp;
//    sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
//    scroller->setScrollerProperties(sp);

    // 设置文本文案
    QLabel *txt = new QLabel("", this);
    txt->setText(tr("Setting up identity identification... \nSetting up discretionary access... \nSetting up privilege separation... \nSetting up data integrity... \nTurning on firewall protection... \nChanging the password policy to high... \nTurning on system resource monitoring (show warnings when the available CPU and memory are decreased to 10%)... \nTurning on trusted measurement (trusted hardware required)... \nSetting up system protection measures... \nOther related settings...")); // 根据类型设置文本文案内容
    txt->setAccessibleName("viewWidget_txt");
    txt->setWordWrap(true);
    txt->setMinimumWidth(400);
    txt->setContentsMargins(10, 5, 10, 5);
    txt->setAlignment(Qt::AlignLeft);
//    DFontSizeManager::instance()->bind(txt, DFontSizeManager::T8);
    scrollArea->setWidget(txt); // 添加到滑动条控件中

    // 旋转控件及布局
    DSpinner *spinner = new DSpinner(contentWidget);
    spinner->setAccessibleName("contentWidget_spinner");
    spinner->setFixedSize(20, 20);
    spinner->start();

    QHBoxLayout *spLayout = new QHBoxLayout(contentWidget);
    spLayout->addWidget(spinner, Qt::AlignCenter);
    contentLayout->addSpacing(10);
    contentLayout->addLayout(spLayout);

    // 添加到DDialog
    addContent(contentWidget);
}

// 关闭窗口事件
void ConfiguratingSystemLevelDialog::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    deleteLater();
}
